package xyz.yuanwl.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
public class AppController {

	@GetMapping("/")
	public String index() {
		return "index";
	}

	@GetMapping(value = "/login")
	public String login() {
		return "login";
	}

	@GetMapping("/home")
	public String homePage(Model model) {
		model.addAttribute("user", getUsername());
		model.addAttribute("role", getAuthorities());
		return "home";
	}

	@GetMapping(value = "/admin")
	public String adminPage(Model model) {
		model.addAttribute("user", getUsername());
		model.addAttribute("role", getAuthorities());
		return "admin";
	}

	@GetMapping(value = "/dba")
	public String dbaPage(Model model) {
		model.addAttribute("user", getUsername());
		model.addAttribute("role", getAuthorities());
		return "dba";
	}

	@GetMapping(value = "/accessDenied")
	public String accessDeniedPage(Model model) {
		model.addAttribute("user", getUsername());
		model.addAttribute("role", getAuthorities());
		return "accessDenied";
	}


	@GetMapping(value = "/logout")
	public String logoutPage(HttpServletRequest request, HttpServletResponse response) {
		//Authentication是一个接口，表示用户认证信息
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		//如果用户认知信息不为空，注销
		if (auth != null) {
			//退出并清除登录用户认证信息
			new SecurityContextLogoutHandler().logout(request, response, auth);
		}
		//重定向到login页面
		return "redirect:/login?logout";
	}

	/**
	 * 获得当前用户名称
	 */
	private String getUsername() {
		//从SecurityContex中获得Authentication对象代表当前用户的信息
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		System.err.println("username = " + username);
		return username;
	}

	/**
	 * 获得当前用户权限
	 */
	private String getAuthorities() {
		//获得Authentication对象，表示用户认证信息。
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		List<String> roles = new ArrayList<>();
		//将角色名称添加到List集合
		for (GrantedAuthority a : authentication.getAuthorities()) {
			roles.add(a.getAuthority());
		}
		System.err.println("role = " + roles);
		return roles.toString();
	}

}
